﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;

namespace PointTracker
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        public string classCRN;

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            {
                if (Session["User"] == null)
                {
                    Response.Redirect("/");
                }
                else if (((User)Session["User"]).role != UserRole.Professor)
                {
                    Response.Redirect("/");
                }

            }

            classCRN = Request.QueryString["class"];
            if (Request.QueryString.Count > 0)
            {
                TextBoxCRN.Text = classCRN;
                TextBoxBatchCRN.Text = classCRN;
            }
        }

        public void AddSingleStudent(Object sender, EventArgs e)
        {
            if (!TextBoxFname.Text.Trim().Equals("") &&
                !TextBoxLname.Text.Trim().Equals("") &&
                !TextBoxDu_id.Text.Trim().Equals("") &&
                !TextBoxCRN.Text.Trim().Equals(""))
            {
                List<Student> tempL = new List<Student>(1);
                tempL.Add(new Student(TextBoxFname.Text, TextBoxLname.Text, TextBoxDu_id.Text, 0, Int32.Parse(TextBoxCRN.Text)));
                Boolean successful = PointTracker.DatabaseManager.addStudents(tempL, Int32.Parse(TextBoxCRN.Text));
                if (successful)
                {
                    Response.Redirect("~/Professor.aspx");
                }
                else
                {
                    Response.Redirect("~/Error/Default.aspx");
                }
            }
        }

        public void AddBatchStudent(Object sender, EventArgs e)
        {
            string batchUploadText = TextBoxBatchUpload.Text;
            int batchUploadCRN = Int32.Parse(TextBoxBatchCRN.Text);

            if (!batchUploadText.Trim().Equals(""))
            {
                Boolean goodBatchFormat = true;
                string[] students = Regex.Split(batchUploadText, "\r\n");

                List<Student> tempL = new List<Student>();
                for(int x  = 0; x < students.Length && goodBatchFormat; x++)
                {
                    string[] studentLine = Regex.Split(students[x], ",");
                    if (studentLine.Length != 3)
                        goodBatchFormat = false;
                    //else
                        //tempL.Add(new Student(studentLine[1], studentLine[0], studentLine[2], 0, batchUploadCRN));
                        //Response.Write(studentLine[1] + ", " + studentLine[0] + ", " + studentLine[2] + ", " + "0, " + batchUploadCRN.ToString());
                }

                if (goodBatchFormat)
                {
                    Boolean successful = PointTracker.Student.addStudents(batchUploadText, batchUploadCRN);//PointTracker.DatabaseManager.addStudents(tempL, batchUploadCRN);
                    if (successful)
                    {
                        Response.Redirect("~/Professor.aspx");
                    }
                    else
                    {
                        Response.Redirect("~/Error/Default.aspx");
                    }
                }
            }
        }
    }
}